# -*- coding: utf-8 -*-
import time
from logger import logger
import random


class YuLing:
    dm = object
    log = object
    count = 50

    def __init__(self, dm, count):
        self.dm = dm
        self.log = logger()
        self.log.info("开始刷御灵")
        self.count = count

    def run(self):
        i = self.count
        while i > 0:
            if self.clickBattle() == 1:
                i = i - 1
                print("第%d次挑战" % (self.count - i))
            self.clickend()
            self.clickXunShan()
            self.clickBeforeEnd()
            self.clickFailEnd()
            self.sleepRandom()

    def clickBattle(self):
        dm = self.dm
        ret = dm.FindMultiColor(781, 404, 914, 491, "675134-000000", "18|-5|f3b25e,-7|7|c3914f,23|-3|906d40", 1, 1, 831,
                                439)
        if ret[0] > 0:
            self.sleepRandom()
            self.log.info('点击参与战斗')
            dm.MoveTo(843, 442)
            dm.LeftClick()
            time.sleep(15)
            return 1
        return 0

    def clickend(self):
        dm = self.dm
        ret = dm.FindMultiColor(451, 423, 651, 523, "2c1813-000000", "21|18|0e0101,-17|19|0b0101,-20|-4|2e1713", 1, 1,
                                552, 477)
        if ret[0] > 0:
            self.sleepRandom()
            self.log.info('点击结束时候的红蛋')
            dm.MoveTo(843, 442)
            dm.LeftClick()

    # 点击接受悬赏
    def clickXunShan(self):
        dm = self.dm
        ret = dm.FindMultiColor(678, 323, 826, 423, "5cb667-000000", "3|-7|77b97e,15|-14|5ab665,-13|-4|60ba6b",
                                1, 1, 753, 373)
        if ret[0] > 0:
            self.sleepRandom()
            self.log.info('接受悬赏')
            dm.MoveTo(753, 373)
            dm.LeftClick()
            time.sleep(1)
            return 1
        else:
            return 0

    def clickBeforeEnd(self):
        dm = self.dm
        ret = dm.FindMultiColor(299, 83, 502, 209, "8e1a11-000000", "48|9|a01b11,13|37|cd917d,9|-12|892217",
                                1, 1, 414, 120)
        if ret[0] > 0:
            self.sleepRandom()
            self.log.info('战斗胜利时结束')
            dm.MoveTo(460, 130)
            dm.LeftClick()
            time.sleep(6)
            self.clickXunShan()
            dm.MoveTo(1026, 300)
            dm.LeftClick()
            time.sleep(3)

    def clickFailEnd(self):
        dm = self.dm
        ret = dm.FindMultiColor(289, 92, 489, 262, "655b71-000000", "8|22|6e647d,-13|-2|251114,6|-3|635a6f", 1, 1, 397,
                                176)
        if ret[0] > 0:
            self.sleepRandom()
            self.log.info('点击失败时候')
            dm.MoveTo(400, 115)
            dm.LeftClick()
            time.sleep(3)

    def sleepRandom(self):
        time.sleep(random.random() * 2)
